* Re-run CAP format analysis as subsample model
* 4/30/2025

clear all
set more off

program main
	twfe_regressions_cap_format
	test_difference
end

program twfe_regressions_cap_format // regress separately by format of CAP
	local indiv_controls = "i.sex i.race i.hispan i.edcat age i.diffphys"
	local state_snap_controls = "i.has_bbce i.has_call i.has_faceini i.has_facerec i.has_oapp i.has_no_fp"
	
	use "$for_analysis/state_for_regressions_actual_ssi", clear
	bys statefip: gen ever_cap = (!mi(startyear)) // state-level indicator for ever having the CAP
	
	gen capformat = 1 if statefip == 36 | statefip == 25 | statefip == 42	// AE standard CAPs
	replace capformat = 2 if standard == 1 & capformat != 1		// non-AE standard CAPs
	replace capformat = 3 if standard == 0 & ever_cap == 1	// modified CAPs
	replace capformat = 0 if standard == 0 & ever_cap == 0	// no CAP

	lab var inter "Treated x Post"
	
	di "AE standard CAPs"
	reghdfe snap i.inter `indiv_controls' state_unemp `state_snap_controls' ///
		[w = perwt] if capformat == 1, a(i.statefip i.year) vce(cl statefip)
	est sto ae
	
	di "non-AE standard CAPs"
	reghdfe snap i.inter `indiv_controls' state_unemp `state_snap_controls' ///
		[w = perwt] if capformat == 2, a(i.statefip i.year) vce(cl statefip)
	est sto standard
	
	di "modified CAPs"
	reghdfe snap i.inter `indiv_controls' state_unemp `state_snap_controls' ///
		[w = perwt] if capformat == 3, a(i.statefip i.year) vce(cl statefip)
	est sto modified
	
	* generate table
	esttab ae standard modified ///
		using "$output/subsample_cap_format", replace ///
		keep(1.inter) order(1.inter) ///
		varlabels(1.inter "Treated x Post") ///
		se(%5.3f) star(* 0.10 ** 0.05 *** 0.01) stats(N, ///
		labels("N" ) ///
		fmt("%9.0fc" "%9.0fc" "%9.0fc" "%9.0fc" "%9.0fc" "%9.0fc")) scalars() ///
		nomtitles label nonote booktabs
end

program test_difference
	local indiv_controls = "i.sex i.race i.hispan i.edcat age i.diffphys"
	local state_snap_controls = "i.has_bbce i.has_call i.has_faceini i.has_facerec i.has_oapp i.has_no_fp"
	
	use "$for_analysis/state_for_regressions_actual_ssi", clear
	bys statefip: gen ever_cap = (!mi(startyear)) // state-level indicator for ever having the CAP
	
	gen capformat = 1 if statefip == 36 | statefip == 25 | statefip == 42	// AE standard CAPs
	replace capformat = 2 if standard == 1 & capformat != 1		// non-AE standard CAPs
	replace capformat = 3 if standard == 0 & ever_cap == 1	// modified CAPs
	replace capformat = 0 if standard == 0 & ever_cap == 0	// no CAP

	lab var inter "Treated x Post"
	
	* note: need to set the same base state for each regression for suest to work
	di "AE standard CAPs"
	qui reg snap i.inter ib1.statefip i.year `indiv_controls' state_unemp `state_snap_controls' ///
		[w = perwt] if capformat == 1
	est sto ae
	
	di "non-AE standard CAPs"
	qui reg snap i.inter ib1.statefip i.year `indiv_controls' state_unemp `state_snap_controls' ///
		[w = perwt] if capformat == 2
	est sto standard
	
	di "modified CAPs"
	qui reg snap i.inter ib1.statefip i.year `indiv_controls' state_unemp `state_snap_controls' ///
		[w = perwt] if capformat == 3
	est sto modified
	
	suest ae standard modified, vce(cl statefip)
	lincom [ae_mean]1.inter - [standard_mean]1.inter
	local ae_standard_b = `r(estimate)'
	local ae_standard_se = `r(se)'
	
	lincom [ae_mean]1.inter - [modified_mean]1.inter
	local ae_modified_b = `r(estimate)'
	local ae_modified_se = `r(se)'
	
	lincom [standard_mean]1.inter - [modified_mean]1.inter
	local standard_modified_b = `r(estimate)'
	local standard_modified_se = `r(se)'
	
	mat results = (`ae_standard_b', `ae_modified_b', `standard_modified_b') \ ///
		(`ae_standard_se', `ae_modified_se', `standard_modified_se')
	mat li results
	local rownames = "Difference (SE)"
	mat rownames results = `rownames'
	
	esttab matrix(results, fmt(%9.3fc)) ///
		using "$output/subsample_diff_cap_format", replace ///
		label nomtitle nonote collabels(none) booktabs compress
end


* Execute
main
